﻿using Canon;
using EDSDKLib;
using Kaliko.ImageLibrary;
using Kaliko.ImageLibrary.Filters;
using Karbel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ConfExpert.EOS
{
    public partial class Liste : Form
    {
        public Liste()
        {
            InitializeComponent();
        }

        SDKHandler CameraHandler;


        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseSession();
            CameraHandler.Dispose();
        }

        void CameraHandler_CameraHasShutdown(object sender, EventArgs e)
        {
            CloseSession();
        }

        private void CloseSession()
        {
            CameraHandler.CloseSession();
        }

        DataManager DataManager;
        string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Photos";



        private void Liste_Load(object sender, EventArgs e)
        {
            this.Visible = false;
            this.FormClosing += MainForm_FormClosing;

            using (Login login = new Login())
            {
                DialogResult dr = login.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    this.Visible = true;

                    DataManager = new DataManager(login.ConnectionString);
                    InitGrid();
                    InitCamera();
                }
                else
                    Application.Exit();
            }




        }

        void InitGrid()
        {
            DataTable dt = DataManager.ExecuteDataTable("GetAksPhotoEksik");
            dataGridView1.DataSource = dt;
            dataGridView1.AutoResizeColumns();
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.CellDoubleClick += dataGridView1_CellDoubleClick;
        }

        void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Code = (int)dataGridView1["AksesuarId", e.RowIndex].Value;
            pictureBox1.Image = null;
            CameraHandler.TakePhoto();
        }

        private void InitCamera()
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            else
            {
                DirectoryInfo di = new DirectoryInfo(path);

                foreach (FileInfo fi in di.GetFiles())
                    fi.Delete();
            }

            CameraHandler = new SDKHandler();
            CameraHandler.DownloadCompleted += CameraHandler_DownloadCompleted;
            CameraHandler.CameraHasShutdown += CameraHandler_CameraHasShutdown;
            CameraHandler.ImageSaveDirectory = path;

            List<Camera> CamList = CameraHandler.GetCameraList();

            if (CamList.Count > 0)
            {
                Camera camera = CamList[0];
                this.Text = camera.Info.szDeviceDescription;
                CameraHandler.OpenSession(camera);

                CameraHandler.SetSetting(EDSDK.PropID_SaveTo, (uint)EDSDK.EdsSaveTo.Host);
                CameraHandler.SetCapacity();
            }
        }

        void CameraHandler_DownloadCompleted(string path)
        {
            this.Text = Code.ToString();
            Image bmp = Bitmap.FromFile(path);
            //pictureBox1.Image = bmp.CropFromCenter(500); 
            pictureBox1.Image = bmp;

            if (Code > 0)
            {
                string pathT = path.Replace(".JPG", "-unsharpened.JPG");
                KalikoImage sharpimg = new KalikoImage(path).GetThumbnailImage(427, 284, ThumbnailMethod.Fit);
                sharpimg.ApplyFilter(new UnsharpMaskFilter(1.2, 0.3));
                sharpimg.SaveJpg(pathT, 95);


                byte[] bytes = File.ReadAllBytes(path);
                byte[] bytesT = File.ReadAllBytes(pathT);
                this.Text = DataManager.ExecuteScalar("UpdAksPhoto2", Code, bytes, bytesT).ToString();
                dataGridView1.CurrentRow.Cells["R"].Value = "E";

                Code = 0;
            }
        }

        int Code = 0;
        void Kaiko()
        {
            KalikoImage image = new KalikoImage(@"E:\IMG_0001.JPG");
            image.BackgroundColor = Color.Aquamarine;

            image.GetThumbnailImage(427, 284, ThumbnailMethod.Fit).SaveJpg(@"E:\thumbnail-fit.jpg", 95);

            //image.GetThumbnailImage(100, 140, ThumbnailMethod.Crop).SaveJpg("thumbnail-crop.jpg", 90);

            //image.GetThumbnailImage(100, 140, ThumbnailMethod.Pad).SaveJpg("thumbnail-pad.jpg", 90);

            KalikoImage sharpimg = image.GetThumbnailImage(427, 284, ThumbnailMethod.Fit);  // image.GetThumbnailImage(100, 100, ThumbnailMethod.Crop);
            sharpimg.ApplyFilter(new UnsharpMaskFilter(1.2, 0.3));
            sharpimg.SaveJpg(@"E:\thumbnail-unsharpened.jpg", 95);

            // Apply unsharpen filter (radius = 1.4, amount = 32%)
            //sharpimg.ApplyFilter(new UnsharpMaskFilter(1.4, 0.32));


            sharpimg.ApplyFilter(new DesaturationFilter());
            sharpimg.SaveJpg(@"E:\thumbnail-gray.jpg", 90);

            sharpimg.ApplyFilter(new ContrastFilter(30));
            sharpimg.SaveJpg(@"E:\thumbnail-contrast.jpg", 90);


        }
      

    }
}
